/**
 * 
 */
package com.objectdraw.server.tools.data;

import java.awt.Font;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.List;

/**
 * POJO representation of a Drawing object
 * @author Harrison
 */
public class DrawingObject {
	private List<String> coords = new ArrayList<String>();
	private String text = "";
	private String color = "";
	private String tool = "";
	
	/**
	 * Return the tool used to create this object
	 * @return
	 */
	public String getTool(){
		return tool;
	}
	/**
	 * Set the tool for used to create this object
	 * @param tool
	 */
	public void setTool(String tool){
		this.tool = tool;
		if(this.tool == null){
			this.tool = "";
		}
	}
	
	/**
	 * Add coordinates needed to draw this object
	 * @param x
	 * @param y
	 */
	public void AddCoords(String x, String y){
		if((!"".equals(x)) && (!"".equals(y))){
			coords.add(x + " " + y);
		}
	}
	
	/**
	 * Returns true if the point is in the object
	 * @return
	 */
	public boolean isPointInObject(int x, int y){
		if(text.length()>0){
			String[] args=coords.get(0).split(" ");
			int sx=Integer.parseInt(args[0]);
			int sy=Integer.parseInt(args[1]);
			Font font = new Font("Serif", Font.BOLD, 24);
			return 
				x<sx+Toolkit.getDefaultToolkit().getFontMetrics(font).stringWidth(text) &&
				x>sx &&
				y<sy+Toolkit.getDefaultToolkit().getFontMetrics(font).getHeight()*1/3 &&
				y>sy-Toolkit.getDefaultToolkit().getFontMetrics(font).getHeight()*2/3;
		}else{
			boolean xtrue=false;
			boolean ytrue=false;
			int xmax=Integer.MIN_VALUE;
			int xmin=Integer.MAX_VALUE;
			int ymax=Integer.MIN_VALUE;
			int ymin=Integer.MAX_VALUE;
			for(int i=0;i<coords.size();i++){
				String[] args=coords.get(i).split(" ");
				int cx =Integer.parseInt(args[0]);
				int cy =Integer.parseInt(args[1]);
				if(cx>xmax)xmax=cx;
				if(cy>ymax)ymax=cy;
				if(cx<xmin)xmin=cx;
				if(cy<ymin)ymin=cy;
				xtrue=xtrue || x<xmax && x>xmin;
				ytrue=ytrue || y<ymax && y>ymin;
				if(xtrue&&ytrue)break;
			}
			return xtrue&&ytrue;
		}
	}
	
	/**
	 * Returns a list of all coordinates needed to draw this object
	 * @return
	 */
	public List<String> getCoords(){
		return coords;
	}
	
	/**
	 * Set the text for this object
	 * @param text
	 */
	public void setText(String text){
		this.text = text;
	}
	
	/**
	 * Returns the text associated with this object
	 * @return
	 */
	public String getText(){
		return text;
	}
	
	/**
	 * Returns the color required to draw this object
	 * @return
	 */
	public String getColor(){
		return color;
	}
	/**
	 * Set the color required to draw this object
	 * @param color
	 */
	public void setColor(String color){
		this.color = color;
		if(this.color == null){
			this.color = "";
		}
	}
	
	/**
	 * returns the object in string format
	 */
	@Override
	public String toString(){
		StringBuilder sb = new StringBuilder();
		sb.append(tool);
		sb.append(" ");
		sb.append(color);
		sb.append(" ");
		for(String s: coords){
			sb.append(s);
			sb.append(" ");
		}
		
		if(!"".equals(text)){
			sb.append(text);
		}
		
		return sb.toString().trim();
		
	}

	public void move(int x, int y) {
		for(int i=0;i<coords.size();i++){
			String[] args=coords.get(i).split(" ");
			coords.set(i, (Integer.parseInt(args[0])+x)+" "+(Integer.parseInt(args[1])+y));
		}
	}
}
